import React from 'react'
import { Menu, Icon } from 'antd';
import { Link } from 'react-router-dom'
import { Component } from 'react';
const SubMenu = Menu.SubMenu;

const arcs = [
  {
    title: '添加',
    id: 'taskAdd'
  },
  {
    title: '任务列表',
    id: 'taskList'
  },
  {
    title: '作业列表',
    id: 'jobList'
  }
];
type Props = {
  base: string
}

class LeftMenu extends React.Component  {
  state = {
    current: '1',
    openKeys: [],
  }
  props:Props
  constructor (props: Props) {
    super(props)
    this.props=props;
  }

  handleClick = (e) => {
    this.setState({ current: e.key });
  }
  onOpenChange = (openKeys) => {
    const state = this.state;
    const latestOpenKey = openKeys.find(key => !(state.openKeys.indexOf(key) > -1));
    const latestCloseKey = state.openKeys.find(key => !(openKeys.indexOf(key) > -1));

    let nextOpenKeys = [];
    if (latestOpenKey) {
      nextOpenKeys = this.getAncestorKeys(latestOpenKey).concat(latestOpenKey);
    }
    if (latestCloseKey) {
      nextOpenKeys = this.getAncestorKeys(latestCloseKey);
    }
    this.setState({ openKeys: nextOpenKeys });
  }
  getAncestorKeys = (key) => {
    const map = {
      sub3: ['sub2'],
    };
    return map[key] || [];
  }
  render() {
    const { base} = this.props
    return (
      <div>
      <Menu
        mode="inline"
        openKeys={this.state.openKeys}
        selectedKeys={[this.state.current]}
        style={{ width: 340 }}
        onOpenChange={this.onOpenChange}
        onClick={this.handleClick}
      >
        <SubMenu key="sub1" title={<span><Icon type="mail" /><span>设备健康模型</span></span>}>
          { arcs.map((arc, i) => {
            return <Menu.Item key={arc.id}>
              <Link to={`${base}/${arc.id}`}>
                  {arc.title}
              </Link>
            </Menu.Item>
          })
          }
        </SubMenu>
      </Menu>
        </div>
    );
  }
}

export default LeftMenu
